export class IndexService {
  constructor() { }
  verifyStep(activeIndex, formData) {
    let result = {};
    let rules = [];
    switch (activeIndex) {
      // case 1:
      //   rules = [
      //     { rule: formData.supplier_id !== '', message: '请选择供应商' },
      //     { rule: formData.contract_id !== '', message: '请选择合同' }
      //   ];
      //   result = checkRule(rules);
      //   break;
      case 1:
        rules = [
          { rule: formData.return_reason.reason_sel !== '', message: '请选择退货原因' },
          { rule: formData.return_reason.reason_text !== '', message: '请填写具体原因' },
          { rule: formData.return_reason.reason_imgs.length > 0, message: '请选择图片' },
          { rule: formData.return_goods.length > 0, message: '请添加产品' }
        ];
        result = checkRule(rules);
        break;
      // case 2:
      //   rules = [{ rule: formData.exchange_goods.length > 0, message: '请选择换货产品' }];
      //   result = checkRule(rules);
      //   if (result.pass === false) {
      //     return result;
      //   }
      //   break;
      case 2:
        rules = [
          {
            rule: formData.full_address && formData.full_address.id && formData.full_address.id !== '',
            message: '请选择退货人信息'
          }
        ];
        result = checkRule(rules);
        if (result.pass === false) {
          return result;
        }
        break;
      default:
        result.pass = true;
        break;
    }
    return result;
  }
}

/**
 * 检查规则，遇到rule=false的情况，立即扔出结果
 * @param {rule:bool, message:string} verifyRules
 */
function checkRule(verifyRules) {
  let result = { pass: true, message: '' };
  for (let i = 0; i < verifyRules.length; i++) {
    const element = verifyRules[i];
    if (element.rule === true) {
      result.pass = true;
    } else {
      result.message = element.message;
      result.pass = false;
      return result;
    }
  }
  return result;
}
